{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 12 while语句"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.【选择】下述while循环执行的次数为多少次？（ ）  \n",
    "`\n",
    "k = 100 \n",
    "while k > 1: \n",
    "    print(k) \n",
    "    k = k // 2`\n",
    "\n",
    "A.3  \n",
    "B.4  \n",
    "C.5  \n",
    "D.6"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.【判断】Python中可以用for、while、do-while来完成循环语句。（ ）\n",
    "\n",
    "A.√  \n",
    "B.×"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：B"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.【选择】下列Python语句正确的是：（ ）\n",
    "\n",
    "A.`min = i if i<j j`   \n",
    "B.`max = a>b ? a : b`   \n",
    "C.`for i in range(3) print(i)`  \n",
    "D.`while False : pass`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.【填空】请写出以下代码的运行结果：（ ）  \n",
    "`\n",
    "i = 1\n",
    "sum = 0\n",
    "while i<=12:\n",
    "    sum += i\n",
    "    i += 1\n",
    "    if i%3 == 0:\n",
    "        continue\n",
    "    if i == 10:\n",
    "        break\n",
    "print(sum)`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "答案：45"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEQAAAA7CAYAAADCZyymAAAGiklEQVRoBe1aaWxNTRh+ykeb2JfWvqVNU1KN9a+1FSKE2iIS/EMQO4kgxT8kQj+pv/yxxJJYikqsv4jQlCpa1K61r631/fJ8k+mZOz29i3sdN993TnLuPfPOO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"source": [
    "5.【编程】请用while语句编程绘制下列图形：  \n",
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "i = 1\n",
    "while i <= 3 :\n",
    "    a = (3 - i)*\" \"\n",
    "    b = (2*i - 1)*\"*\"\n",
    "    print(a+b)\n",
    "    i += 1`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6.【编程】请用while解决鸡兔同笼问题，假设有30个头，84只脚。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "head = 30\n",
    "feet = 84\n",
    "rabbit = chook = 0\n",
    "while (rabbit <= feet/4):\n",
    "    chook = head - rabbit\n",
    "    if (rabbit*4 + chook*2 == feet):\n",
    "        print(\"鸡有：\"+str(chook)+\"只\")\n",
    "        print(\"兔有：\"+str(rabbit)+\"只\")\n",
    "        break\n",
    "    rabbit += 1`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7.【编程】求322和115的最大公约数。（用辗转相除法）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考答案：  \n",
    "`\n",
    "a = 322\n",
    "b = 115\n",
    "while(b != 0):\n",
    "    i = a%b\n",
    "    a = b\n",
    "    b = i\n",
    "print(\"它们的最大公约数是：\"+str(a))`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
